package com.jsmile.cloud.authc.help;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jsmile.cloud.authc.api.req.sys.ReqSysHelpQuery;
import com.jsmile.cloud.authc.api.req.sys.ReqSysHelpUpdate;
import com.jsmile.cloud.authc.api.res.ResSysHelp;
import com.jsmile.cloud.authc.help.model.SysHelp;
import com.jsmile.cloud.authc.help.repo.SysHelpRepository;
import com.jsmile.cloud.authc.help.repo.SysHelpSearchRepository;
import com.jsmile.mall.api.exception.ExceptionKit;

import cn.hutool.core.bean.BeanUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@AllArgsConstructor
public class SysHelpApplicationService {

    private final SysHelpSearchRepository sysHelpSearchRepository;
    private final SysHelpRepository sysHelpRepository;

    public IPage<ResSysHelp> getPageList(ReqSysHelpQuery reqSysHelpQuery) {
        ExceptionKit.checkNotNull(reqSysHelpQuery.getCurTenantId(), "租户id不能为空!");
        log.info("部门查询:{]", reqSysHelpQuery);
        return sysHelpSearchRepository.pageQuery(reqSysHelpQuery).convert(x -> BeanUtil.toBean(x, ResSysHelp.class));
    }

    public List<ResSysHelp> list(ReqSysHelpQuery reqSysHelpQuery) {
        List<SysHelp> list = sysHelpSearchRepository.list(reqSysHelpQuery);
        if (CollectionUtils.isEmpty(list))
            return null;
        return list.stream().map(x -> BeanUtil.toBean(x, ResSysHelp.class)).collect(Collectors.toList());
    }

    public boolean saveHelp(ReqSysHelpUpdate reqSysHelpUpdate) {
        return sysHelpRepository.save(SysHelp.create(reqSysHelpUpdate));
    }

    public boolean updateHelp(ReqSysHelpUpdate reqSysHelpUpdate) {
        return sysHelpRepository.updateById(SysHelp.create(reqSysHelpUpdate));
    }

    public boolean enableHelp(ReqSysHelpUpdate reqSysHelpUpdate) {
        return sysHelpRepository.updateById(SysHelp.create(reqSysHelpUpdate));
    }

    public boolean deleteHelp(ReqSysHelpUpdate reqSysHelpUpdate) {
        return sysHelpRepository.removeById(reqSysHelpUpdate.getId());
    }
}
